package cn.ming.wfchat.app.me.activity;

import android.view.View;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.adapter.EarningsRecordAdapter;
import cn.ming.wfchat.app.me.activity.model.EarningsRecordBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.utils.Tools;

/**
 * @author luoming
 * created at 2020/8/11 3:04 PM
 * 收益记录
 */
public class EarningsRecordActivity extends WfcBaseActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    EarningsRecordAdapter earningsRecordAdapter;
    int page = 1;
    private List<EarningsRecordBean.CoinLogBean> moneyFlowingWaterBeanList;
    private boolean isshuai = true;

    @Override

    protected int contentLayout() {
        return R.layout.earnings_record_activity;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        setTitle("收益记录");
        setTvRight("提现记录");
        initView();
        initDate();
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.jump(EarningsRecordActivity.this, WithdrawalRecordActivity.class, true);
            }
        });
    }

    void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        moneyFlowingWaterBeanList = new ArrayList<>();
        earningsRecordAdapter = new EarningsRecordAdapter(moneyFlowingWaterBeanList);
        recyclerView.setAdapter(earningsRecordAdapter);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setListener(new SpringView.OnFreshListener() {
            public void onRefresh() {
                page = 1;
                initDate();
            }

            @Override
            public void onLoadmore() {
                if (isshuai) {
                    page++;
                    initDate();
                } else {
                    springView.onFinishFreshAndLoad();
                }

            }
        });

    }

    void initDate() {
        AppService.Instance().coinLog(page, new AppService.coinLogCallback() {
            @Override
            public void onUiSuccess(EarningsRecordBean assetsBean) {
                springView.onFinishFreshAndLoad();
                if (assetsBean.getCoinLog() != null && assetsBean.getCoinLog().size() > 0) {
                    if (page == 1) {
                        moneyFlowingWaterBeanList = assetsBean.getCoinLog();
                    } else {
                        moneyFlowingWaterBeanList.addAll(assetsBean.getCoinLog());
                    }
                    if (assetsBean.getCoinLog().size() > 9) {
                        isshuai = true;
                    } else {
                        isshuai = false;
                    }
                } else {
                    moneyFlowingWaterBeanList.clear();
                    isshuai = false;
                }
                earningsRecordAdapter.setNewData(moneyFlowingWaterBeanList);
            }

            @Override
            public void onUiFailure(int code, String msg) {

            }
        });
    }

}
